
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 07/05/2014 09:46:30
-- Generated from EDMX file: H:\SPORTSALE\UI\Data\Model.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [SPORTSALE];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Buys_Product]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Compras] DROP CONSTRAINT [FK_Buys_Product];
GO
IF OBJECT_ID(N'[dbo].[FK_Buys_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Compras] DROP CONSTRAINT [FK_Buys_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_Comentarios_Usuarios]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Comentarios] DROP CONSTRAINT [FK_Comentarios_Usuarios];
GO
IF OBJECT_ID(N'[dbo].[FK_Comments_Sale]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Comentarios] DROP CONSTRAINT [FK_Comments_Sale];
GO
IF OBJECT_ID(N'[dbo].[FK_Product_Sport]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Producto] DROP CONSTRAINT [FK_Product_Sport];
GO
IF OBJECT_ID(N'[dbo].[FK_Ratings_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Ratings] DROP CONSTRAINT [FK_Ratings_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_Sale_Product]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Ventas] DROP CONSTRAINT [FK_Sale_Product];
GO
IF OBJECT_ID(N'[dbo].[FK_SaleReports_Sale]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Reportes] DROP CONSTRAINT [FK_SaleReports_Sale];
GO
IF OBJECT_ID(N'[dbo].[FK_Sport_SportCategories]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Deportes] DROP CONSTRAINT [FK_Sport_SportCategories];
GO
IF OBJECT_ID(N'[dbo].[FK_Usuarios_Persona]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Usuarios] DROP CONSTRAINT [FK_Usuarios_Persona];
GO
IF OBJECT_ID(N'[dbo].[FK_Usuarios_Roles]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Usuarios] DROP CONSTRAINT [FK_Usuarios_Roles];
GO
IF OBJECT_ID(N'[dbo].[FK_Ventas_Usuarios]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Ventas] DROP CONSTRAINT [FK_Ventas_Usuarios];
GO
IF OBJECT_ID(N'[dbo].[FK_Ventas_Valoraciones]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Ventas] DROP CONSTRAINT [FK_Ventas_Valoraciones];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Categorias]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Categorias];
GO
IF OBJECT_ID(N'[dbo].[Comentarios]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Comentarios];
GO
IF OBJECT_ID(N'[dbo].[Compras]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Compras];
GO
IF OBJECT_ID(N'[dbo].[Deportes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Deportes];
GO
IF OBJECT_ID(N'[dbo].[Persona]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Persona];
GO
IF OBJECT_ID(N'[dbo].[Producto]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Producto];
GO
IF OBJECT_ID(N'[dbo].[Ratings]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Ratings];
GO
IF OBJECT_ID(N'[dbo].[Reportes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Reportes];
GO
IF OBJECT_ID(N'[dbo].[Roles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Roles];
GO
IF OBJECT_ID(N'[dbo].[sysdiagrams]', 'U') IS NOT NULL
    DROP TABLE [dbo].[sysdiagrams];
GO
IF OBJECT_ID(N'[dbo].[Usuarios]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Usuarios];
GO
IF OBJECT_ID(N'[dbo].[Valoraciones]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Valoraciones];
GO
IF OBJECT_ID(N'[dbo].[Ventas]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Ventas];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Categorias'
CREATE TABLE [dbo].[Categorias] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Nombre] varchar(50)  NOT NULL,
    [Descripcion] varchar(50)  NULL
);
GO

-- Creating table 'Comentarios'
CREATE TABLE [dbo].[Comentarios] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [IdVenta] int  NOT NULL,
    [Descripcion] varchar(50)  NOT NULL,
    [IdUsuario] int  NOT NULL,
    [Fecha] datetime  NOT NULL
);
GO

-- Creating table 'Compras'
CREATE TABLE [dbo].[Compras] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [IdUsuario] int  NOT NULL,
    [IdProducto] int  NOT NULL,
    [Fecha] datetime  NOT NULL,
    [Codigo] varchar(50)  NOT NULL,
    [Estado] varchar(50)  NULL
);
GO

-- Creating table 'Deportes'
CREATE TABLE [dbo].[Deportes] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Nombre] varchar(50)  NOT NULL,
    [IdCategoria] int  NOT NULL
);
GO

-- Creating table 'Persona'
CREATE TABLE [dbo].[Persona] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Nombre] varchar(50)  NOT NULL,
    [Apellido] varchar(50)  NOT NULL,
    [Tel__Particular] int  NULL,
    [Tel__Celuar] int  NULL,
    [Calle] varchar(50)  NOT NULL,
    [NumeroCalle] varchar(50)  NOT NULL,
    [DNI] int  NOT NULL
);
GO

-- Creating table 'Producto'
CREATE TABLE [dbo].[Producto] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Nombre] varchar(50)  NOT NULL,
    [Descripcion] varchar(50)  NOT NULL,
    [Stock] int  NOT NULL,
    [IdDeporte] int  NOT NULL,
    [Precio] float  NOT NULL,
    [Imagen] varchar(50)  NOT NULL
);
GO

-- Creating table 'Ratings'
CREATE TABLE [dbo].[Ratings] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Valor] int  NOT NULL,
    [IdUsuario] int  NOT NULL
);
GO

-- Creating table 'Reportes'
CREATE TABLE [dbo].[Reportes] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [IdVenta] int  NOT NULL,
    [Descripcion] varchar(50)  NOT NULL,
    [Valor] int  NOT NULL
);
GO

-- Creating table 'Roles'
CREATE TABLE [dbo].[Roles] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Nombre] varchar(50)  NOT NULL
);
GO

-- Creating table 'sysdiagrams'
CREATE TABLE [dbo].[sysdiagrams] (
    [name] nvarchar(128)  NOT NULL,
    [principal_id] int  NOT NULL,
    [diagram_id] int IDENTITY(1,1) NOT NULL,
    [version] int  NULL,
    [definition] varbinary(max)  NULL
);
GO

-- Creating table 'Usuarios'
CREATE TABLE [dbo].[Usuarios] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Username] varchar(20)  NOT NULL,
    [Password] varchar(20)  NOT NULL,
    [IdRol] int  NOT NULL,
    [Puntos] int  NOT NULL,
    [IdPersona] int  NOT NULL,
    [Email] varchar(50)  NOT NULL,
    [Activo] int  NOT NULL
);
GO

-- Creating table 'Valoraciones'
CREATE TABLE [dbo].[Valoraciones] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [ValoracionVendedor] int  NOT NULL,
    [ValoracionComprador] int  NOT NULL
);
GO

-- Creating table 'Ventas'
CREATE TABLE [dbo].[Ventas] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [IdProducto] int  NOT NULL,
    [IdUsuario] int  NOT NULL,
    [IdValoracion] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Categorias'
ALTER TABLE [dbo].[Categorias]
ADD CONSTRAINT [PK_Categorias]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Comentarios'
ALTER TABLE [dbo].[Comentarios]
ADD CONSTRAINT [PK_Comentarios]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Compras'
ALTER TABLE [dbo].[Compras]
ADD CONSTRAINT [PK_Compras]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Deportes'
ALTER TABLE [dbo].[Deportes]
ADD CONSTRAINT [PK_Deportes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Persona'
ALTER TABLE [dbo].[Persona]
ADD CONSTRAINT [PK_Persona]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Producto'
ALTER TABLE [dbo].[Producto]
ADD CONSTRAINT [PK_Producto]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Ratings'
ALTER TABLE [dbo].[Ratings]
ADD CONSTRAINT [PK_Ratings]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Reportes'
ALTER TABLE [dbo].[Reportes]
ADD CONSTRAINT [PK_Reportes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Roles'
ALTER TABLE [dbo].[Roles]
ADD CONSTRAINT [PK_Roles]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [diagram_id] in table 'sysdiagrams'
ALTER TABLE [dbo].[sysdiagrams]
ADD CONSTRAINT [PK_sysdiagrams]
    PRIMARY KEY CLUSTERED ([diagram_id] ASC);
GO

-- Creating primary key on [Id] in table 'Usuarios'
ALTER TABLE [dbo].[Usuarios]
ADD CONSTRAINT [PK_Usuarios]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Valoraciones'
ALTER TABLE [dbo].[Valoraciones]
ADD CONSTRAINT [PK_Valoraciones]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Ventas'
ALTER TABLE [dbo].[Ventas]
ADD CONSTRAINT [PK_Ventas]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [IdCategoria] in table 'Deportes'
ALTER TABLE [dbo].[Deportes]
ADD CONSTRAINT [FK_Sport_SportCategories]
    FOREIGN KEY ([IdCategoria])
    REFERENCES [dbo].[Categorias]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Sport_SportCategories'
CREATE INDEX [IX_FK_Sport_SportCategories]
ON [dbo].[Deportes]
    ([IdCategoria]);
GO

-- Creating foreign key on [IdUsuario] in table 'Comentarios'
ALTER TABLE [dbo].[Comentarios]
ADD CONSTRAINT [FK_Comentarios_Usuarios]
    FOREIGN KEY ([IdUsuario])
    REFERENCES [dbo].[Usuarios]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Comentarios_Usuarios'
CREATE INDEX [IX_FK_Comentarios_Usuarios]
ON [dbo].[Comentarios]
    ([IdUsuario]);
GO

-- Creating foreign key on [IdVenta] in table 'Comentarios'
ALTER TABLE [dbo].[Comentarios]
ADD CONSTRAINT [FK_Comments_Sale]
    FOREIGN KEY ([IdVenta])
    REFERENCES [dbo].[Ventas]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Comments_Sale'
CREATE INDEX [IX_FK_Comments_Sale]
ON [dbo].[Comentarios]
    ([IdVenta]);
GO

-- Creating foreign key on [IdProducto] in table 'Compras'
ALTER TABLE [dbo].[Compras]
ADD CONSTRAINT [FK_Buys_Product]
    FOREIGN KEY ([IdProducto])
    REFERENCES [dbo].[Producto]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Buys_Product'
CREATE INDEX [IX_FK_Buys_Product]
ON [dbo].[Compras]
    ([IdProducto]);
GO

-- Creating foreign key on [IdUsuario] in table 'Compras'
ALTER TABLE [dbo].[Compras]
ADD CONSTRAINT [FK_Buys_Users]
    FOREIGN KEY ([IdUsuario])
    REFERENCES [dbo].[Usuarios]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Buys_Users'
CREATE INDEX [IX_FK_Buys_Users]
ON [dbo].[Compras]
    ([IdUsuario]);
GO

-- Creating foreign key on [IdDeporte] in table 'Producto'
ALTER TABLE [dbo].[Producto]
ADD CONSTRAINT [FK_Product_Sport]
    FOREIGN KEY ([IdDeporte])
    REFERENCES [dbo].[Deportes]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Product_Sport'
CREATE INDEX [IX_FK_Product_Sport]
ON [dbo].[Producto]
    ([IdDeporte]);
GO

-- Creating foreign key on [IdPersona] in table 'Usuarios'
ALTER TABLE [dbo].[Usuarios]
ADD CONSTRAINT [FK_Usuarios_Persona]
    FOREIGN KEY ([IdPersona])
    REFERENCES [dbo].[Persona]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Usuarios_Persona'
CREATE INDEX [IX_FK_Usuarios_Persona]
ON [dbo].[Usuarios]
    ([IdPersona]);
GO

-- Creating foreign key on [IdProducto] in table 'Ventas'
ALTER TABLE [dbo].[Ventas]
ADD CONSTRAINT [FK_Sale_Product]
    FOREIGN KEY ([IdProducto])
    REFERENCES [dbo].[Producto]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Sale_Product'
CREATE INDEX [IX_FK_Sale_Product]
ON [dbo].[Ventas]
    ([IdProducto]);
GO

-- Creating foreign key on [IdUsuario] in table 'Ratings'
ALTER TABLE [dbo].[Ratings]
ADD CONSTRAINT [FK_Ratings_Users]
    FOREIGN KEY ([IdUsuario])
    REFERENCES [dbo].[Usuarios]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Ratings_Users'
CREATE INDEX [IX_FK_Ratings_Users]
ON [dbo].[Ratings]
    ([IdUsuario]);
GO

-- Creating foreign key on [IdVenta] in table 'Reportes'
ALTER TABLE [dbo].[Reportes]
ADD CONSTRAINT [FK_SaleReports_Sale]
    FOREIGN KEY ([IdVenta])
    REFERENCES [dbo].[Ventas]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SaleReports_Sale'
CREATE INDEX [IX_FK_SaleReports_Sale]
ON [dbo].[Reportes]
    ([IdVenta]);
GO

-- Creating foreign key on [IdRol] in table 'Usuarios'
ALTER TABLE [dbo].[Usuarios]
ADD CONSTRAINT [FK_Usuarios_Roles]
    FOREIGN KEY ([IdRol])
    REFERENCES [dbo].[Roles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Usuarios_Roles'
CREATE INDEX [IX_FK_Usuarios_Roles]
ON [dbo].[Usuarios]
    ([IdRol]);
GO

-- Creating foreign key on [IdUsuario] in table 'Ventas'
ALTER TABLE [dbo].[Ventas]
ADD CONSTRAINT [FK_Ventas_Usuarios]
    FOREIGN KEY ([IdUsuario])
    REFERENCES [dbo].[Usuarios]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Ventas_Usuarios'
CREATE INDEX [IX_FK_Ventas_Usuarios]
ON [dbo].[Ventas]
    ([IdUsuario]);
GO

-- Creating foreign key on [IdValoracion] in table 'Ventas'
ALTER TABLE [dbo].[Ventas]
ADD CONSTRAINT [FK_Ventas_Valoraciones]
    FOREIGN KEY ([IdValoracion])
    REFERENCES [dbo].[Valoraciones]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Ventas_Valoraciones'
CREATE INDEX [IX_FK_Ventas_Valoraciones]
ON [dbo].[Ventas]
    ([IdValoracion]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------